package code08_三大特征;

public class Test01_封装 {
	public static void main(String[] args) {
		User u = new User();
		// u.name = "张三";
		// u.age = 200; // 无效数据
		// u.sex = "妖";

		u.setName("张三");
		u.setAge(20);
//		u.setSex("妖");
		u.setMarried(true);

		u.show();

		// System.out.println(u.getAge());
		System.out.println(u.getSex());
		System.out.println(u.isMarried());
	}
}

/*
 * 用户类
 */
class User {
	/*
	 * 将属性私有化，使用private修饰属性
	 */
	private String name;
	private int age;
	private String sex;
	private boolean married;

	/*
	 * 提供对外公开的访问方法，用于赋值、取值
	 */
	public void setAge(int age) {
		if (age >= 1 && age <= 100) {
			this.age = age;
		} else {
			this.age = 18;
			System.out.println("age无效，已设置为默认值18");
		}
	}

//	public int getAge() {
//		return age;
//	}

//	public void setSex(String sex) {
//		if (sex.equals("男") || sex.equals("女")) {
//			this.sex = sex;
//		} else {
//			System.out.println("sex无效！");
//		}
//	}

	public String getSex() {
		return sex;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public void setMarried(boolean married){
		this.married = married;
	}
	
	public boolean isMarried(){
		return married;
	}
	

	public void show() {
		System.out.println("姓名：" + name);
		System.out.println("年龄：" + age);
		System.out.println("性别：" + sex);
	}
}